package com.heo.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

public class Utils {
	/**
	 * 字符串转换为double类型 四舍五入
	 * 
	 * @param str
	 *            字符串
	 * 
	 * @param scale
	 *            精度
	 * 
	 * @return double
	 */
	public static double stringToDouble(String str, int scale) {
		double d = 0;
		if (checkNull(str)) {
			BigDecimal l = new BigDecimal(str);
			d = l.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		return d;
	}

	/**
	 * 把byte数组转换为字符串
	 * 
	 * @param bytes
	 *            byte数组
	 * 
	 * @return string
	 */
	public static String bytesToString(byte[] bytes) {
		String strBytes = null;
		if (null != bytes && bytes.length > 0) {
			strBytes = new String(bytes);
		}
		return strBytes;
	}

	// 字符串类型的工具方法

	/**
	 * 判断字符串是否有内容 null安全
	 * 
	 * @param str
	 * 
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return StringUtils.isEmpty(str);
	}

	/**
	 * 清除字符串前后空格 null安全
	 * 
	 * @param str
	 * @return
	 */
	public static String trim(String str) {
		return StringUtils.trim(str);
	}

	/**
	 * 替换字符串所有空格、换行符、回车、制表符等
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (Utils.checkNull(str)) {
			Pattern p = Pattern.compile("\\s*|t|r|n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 字符串非空判断
	 * 
	 * @param str
	 * 
	 * @return boolean true:不为空; false：空
	 * 
	 * @throws FMPException
	 */
	public static boolean checkNull(Object str) {
		boolean falg = false;
		if (null != str && !"".equals(str.toString().trim())) {
			falg = true;
		}
		return falg;
	}

	/**
	 * 处理字符串
	 * 
	 * @param obj
	 * @return
	 */
	public static String getStr(Object obj) {
		String str = "";
		if (obj != null && !"".equals(obj)) {
			str = obj.toString().trim();
		}
		return str;
	}

	/**
	 * 从String中取得Long
	 * 
	 * @param str
	 *            String参数
	 * @return Long
	 */
	public static long getLong(Object obj) {
		long lnNumber = 0;
		try {

			lnNumber = Long.parseLong(getStr(obj));
		} catch (Exception e) {
		}
		return lnNumber;
	}

	/**
	 * 把String转换为double
	 * 
	 * @param obj
	 * @return
	 */
	public static double getDouble(Object obj) {
		double d = 0;
		try {
			d = Double.parseDouble("" + obj);
		} catch (Exception e) {
		}
		return d;
	}

	/**
	 * 把String转换为double
	 * 
	 * @param obj
	 * @return
	 */
	public static int getInteger(Object obj) {
		int lnNumber = 0;
		try {

			lnNumber = Integer.parseInt(getStr(obj));
		} catch (Exception e) {
		}
		return lnNumber;
	}

	/**
	 * 把String转换为double
	 * 
	 * @param obj
	 * @return
	 */
	public static short getShort(Object obj) {
		short lnNumber = 0;
		try {

			lnNumber = Short.parseShort(getStr(obj));
		} catch (Exception e) {
		}
		return lnNumber;
	}

	/**
	 * 格式化double
	 * 
	 * @param obj
	 * @return
	 */
	public static double getDoubleFormatDouble(double obj) {
		DecimalFormat df = new DecimalFormat("######0.00");
		return new Double(df.format(obj).toString());
	}

	public static String getDoubleFormat(double account) {
		DecimalFormat df = new DecimalFormat("######0.00");
		return df.format(account);
	}

	/**
	 * 获取误差停止时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getBreakTime(int time) {
		int retTime = 0;
		if (time < 10) {
			retTime = 1;
		} else {
			retTime = 2;
		}
		return retTime;
	}

	/**
	 * 获取工作时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getWorkTime(int workTime) {
		if (workTime > 1) {
			workTime = workTime - 1;
		} else {
			workTime = 1;
		}
		return workTime;
	}

	/**
	 * 取得当前时间戳（精确到秒）
	 * 
	 * @return
	 */
	public static String timeStamp() {
		long time = System.currentTimeMillis();
		String t = String.valueOf(time / 1000);
		return t;
	}

	public static void main(String[] args) {
		JSONObject startMsg = new JSONObject();
		startMsg.put("deviceIMEI", "0000ZHDS0000430");
		startMsg.put("commandId", 30);
		startMsg.put("deviceChannelIndex", 1);
		startMsg.put("startPara", "");
		startMsg.put("callbackPara", "a30f9fd9ef7b22b16191591a25dd563d");
		startMsg.put("appId", "a30f9fd9ef7b22b16191591a25dd563d");
		startMsg.put("callbackExpireTime", 24 * 3600);
		String timeStamp = Utils.timeStamp();
		String timeCode = ByteHelper.intToHexString(Utils.getInteger(timeStamp), 8);
		System.out.println(timeCode);
		System.out.println(ByteHelper.stringToAscii("013824389626"));
	}
}
